import pygame
import sys

from model.Hero import Hero


def check_button(event, button_list):
    for button in button_list:
        if event.key == button:
            return True
    return False


def check_item_button(event):
    for item in Hero.item_list:
        if event.key == item.button:
            item.work()


class GameController:

    upButtons = [pygame.K_UP, pygame.K_w]
    downButtons = [pygame.K_DOWN, pygame.K_s]
    leftButtons = [pygame.K_LEFT, pygame.K_a]
    rightButtons = [pygame.K_RIGHT, pygame.K_d]

    def __init__(self):
        pass

    def update(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if check_button(event, self.upButtons):
                    Hero.direction = [0, -1, 0]
                    Hero.step += 1
                elif check_button(event, self.downButtons):
                    Hero.direction = [0, 1, 1]
                    Hero.step += 1
                elif check_button(event, self.leftButtons):
                    Hero.direction = [-1, 0, 2]
                    Hero.step += 1
                elif check_button(event, self.rightButtons):
                    Hero.direction = [1, 0, 3]
                    Hero.step += 1
                else:
                    check_item_button(event)
